package com.nextgen.services;

import java.util.Date;

import org.apache.log4j.Logger;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import com.nextgen.bean.SellersCategory;
import com.nextgen.mongo.core.MongoInstance;
  
/**  
 * The Class SellerService.  
 */  
@Service  
public class SellerService {  
  
     private static final Logger logger_c = Logger.getLogger(SellerService.class);
     
     /**  
      * Get the SellersCategory by sellersId.  
      *  
      * @param sellerId_p  
      *      the seller id  
      * @return the SellersCategory by id  
      */  
	public SellersCategory getCategoriesBySellerId(String sellerId_p) {
		MongoOperations mongoOperation = MongoInstance.getInstance();
		Query searchUserQuery = new Query(Criteria.where("sellerId").is(sellerId_p));
		
		// find the saved user again.
		SellersCategory sellersCategoryDetails = mongoOperation.findOne(searchUserQuery, SellersCategory.class);
		logger_c.debug("sellersCategory details with id : " + sellerId_p + " is : "  + sellersCategoryDetails);
		return sellersCategoryDetails;
	}
  
     /**  
      * Creates the sellersCategory.  
      *  
      * @param sellersCategory  
      *      the sellersCategory  
      * @return the sellersCategory  
      */  
     public SellersCategory createSellersCategory(SellersCategory sellersCategory) {  
  
          logger_c.debug("Persisting asin in database: " + sellersCategory.toString());  
          MongoOperations mongoOperation = MongoInstance.getInstance();
          mongoOperation.save(sellersCategory);  
  
          return sellersCategory;  
     }  
  
     /**  
      * Update asin.  
      *  
      * @param asin_p  
      *      the asin_p  
      * @return the asin  
      */  
     public SellersCategory updateAsin(SellersCategory sellersCategory) {  
  
          logger_c.debug("Updating asin in database: " + sellersCategory.toString());  
          sellersCategory.setLastUpdated(new Date());
          MongoOperations mongoOperation = MongoInstance.getInstance();
          Query searchUserQuery = new Query(Criteria.where("sellerId").is(sellersCategory.getSellerId()));
          mongoOperation.updateFirst(searchUserQuery, 
                  Update.update("password", "new password"),SellersCategory.class);
  
          return sellersCategory;  
     }
}